package com.sdp.wx.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_repair_team.entity.WxRepairTeam;
import com.sdp.wx.db.wx_repair_team.service.IWxRepairTeamService;
import com.sdp.wx.utils.WxConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-02-04
 */
@RestController
@RequestMapping("/WxRepairTeamController")
public class WxRepairTeamController {
	
	@Autowired
	private Query query;
	
	@Autowired
	private IWxRepairTeamService iWxRepairTeamService;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxRepairTeamService, WxRepairTeam.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	public Result<?> save(@RequestBody WxRepairTeam repairTeam) {
		if(repairTeam==null) {
			return Result.error("维修班组信息为空");
		}
		if(StringUtils.isEmpty(repairTeam.getTeamOrder())) {
			repairTeam.setTeamOrder(1);
			repairTeam.setIsUse(WxConstants.是.getCode());
		}
		if(StringUtils.isEmpty(repairTeam.getKeyId())) {
			//账号不能重复和工号不能重复
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxRepairTeamService, WxRepairTeam.class, new String[] {"orgNo","teamName"}, "keyId", repairTeam, null, true);
			if(r==null || !r.isSuccess()) {
				return Result.error("维修班组名称不能重复");
			}
			repairTeam.setIsUse(WxConstants.是.getCode()	);
			iWxRepairTeamService.save(repairTeam);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxRepairTeamService, WxRepairTeam.class, new String[] {"orgNo","teamName"}, "keyId", null, repairTeam, true);
			if(r==null || !r.isSuccess()) {
				return Result.error("维修班组名称不能重复");
			}
			iWxRepairTeamService.updateById(repairTeam);
		}
		return Result.ok(repairTeam);
	}

}

